package com.api.gbhg.lbj.action;

import com.alibaba.fastjson.JSONObject;
import com.api.gbhg.lbj.dao.ReimbursementDao;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lbj
 * @version 1.0
 * @description: TODO 明细表金额退回
 * @date 2021/3/11 14:58
 */
public class DetailReturnAmountAction implements Action{
    private static final Logger logger = LoggerFactory.getLogger("gbhg");
    private   final PropertyManagementCostDao pmc=new PropertyManagementCostDao();
    @Override
    public String execute(RequestInfo requestInfo) {
        logger.info("进入报销明细表金额退回DetailReturnAmountAction--");
        //请求id
        String requestid = requestInfo.getRequestid();
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        String src = requestInfo.getRequestManager().getSrc();
        logger.info("明细表退回释放预算冻结额度------:"+src+"..."+requestid);
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        if (src.equals("reject")){//退回
            RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);
            rs.next();
            String mainid = rs.getString("id");
            RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[0].getTableDBName(), mainid);
            logger.info("明细表金额退回mainid="+mainid+" 查询结果长度："+recordSet.getArray().size());
            Map<String, String>[] list = new HashMap[recordSet.getArray().size()];
            int i=0;
            while (recordSet.next()) {
                logger.info("退回执行第 "+i+"行");
                list[i] = new HashMap<>();
                String sqbxje = recordSet.getString("sqbxje");
                String bmjjzcfl = recordSet.getString("bmjjzcfl");
                String zcqd = recordSet.getString("zcqd");
                String ysly = recordSet.getString("ysly");
                String fzysrfl = recordSet.getString("fzysrfl");
                String xzkmdm = recordSet.getString("xzkmdm");
                String lcjlid = recordSet.getString("ysxm");
                BigDecimal moneyDj = new BigDecimal(recordSet.getDouble("sqbxje"));
                //BigDecimal tybxje = new BigDecimal(recordSet.getDouble("tybxje"));
                String cxje = recordSet.getString("cxje");//huangwm
                list[i].put("lcjlid", lcjlid);//预算项目
                list[i].put("money", sqbxje);//申请报销金额
                list[i].put("tybxje", sqbxje);//同意报销金额退回为申请金额
                list[i].put("zcqd", zcqd);//支出渠道
                list[i].put("ysly", ysly);//预算来源
                list[i].put("fzysrfl", fzysrfl);//非中央收入分类
                list[i].put("xzkmdm", xzkmdm);//选择科目代码
                list[i].put("bmjjzcfl", bmjjzcfl);//部门经济支出分类 huangwm
                list[i].put("cxje", cxje);//冲销金额 huangwm
                logger.info("预算项目:" + lcjlid + "申请报销金额:" + moneyDj + "部门经济支出分类:" + bmjjzcfl);
                i++;
            }
            logger.info(JSONObject.toJSONString(list));
            ReimbursementDao rei = new ReimbursementDao();
            boolean b = rei.queryJingfei(rst,list, false, 1);//1：退回
            logger.info("本次退回释放结果："+b);
            //            boolean j=true;
            //            if (StringUtils.isNotBlank(xzkmdm)||StringUtils.isNotBlank(ysly)) {
            //                j = rei.jingfeilaiyuandonjie(list, true);
            //            }
            if (!b) {
                rst.rollback();
                logger.info("=====================退回错误=======================");
                requestInfo.getRequestManager().setMessagecontent("退回释放预算失败");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
        }
        RecordSet del = new RecordSet();
        del.execute("delete from uf_bxsjjl where lcid="+requestid);
        rst.commit();
        return SUCCESS;
    }
}
